package com.xiaobao.campusLostFoundAfter.service.impl;

import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.mapper.ViewsMapper;
import com.xiaobao.campusLostFoundAfter.pojo.Views;
import com.xiaobao.campusLostFoundAfter.service.ViewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class ViewsServiceImpl implements ViewsService {
    private static final Logger log = LoggerFactory.getLogger(ViewsServiceImpl.class);
    private final ViewsMapper viewsMapper;

    public ViewsServiceImpl(ViewsMapper viewsMapper) {
        this.viewsMapper = viewsMapper;
    }

    //添加浏览记录
    @Override
    public int addViews(Integer itemId, String type) {
        //先判断这个浏览记录id是否存在
        boolean result = viewsMapper.getViews(itemId, type);
        log.info("result:{}", result);
        if (result) {
            //存在则更新
            return viewsMapper.updateViews(itemId, type);
        } else {
            //不存在则添加
            return viewsMapper.addViews(itemId, type);
        }
    }

    //获取浏览记录
    @Override
    public Result<Views> getViews(Integer itemId) {
        String type = "found";
        return Result.success(viewsMapper.getViewsNumber(itemId, type));
    }

    //获取浏览记录
    @Override
    public Result<Views> getViewsLost(Integer itemId) {
        String type = "lost";
        return Result.success(viewsMapper.getViewsNumber(itemId, type));
    }
}
